﻿using System;
using System.Text;

using System.Net.Mail;

namespace HuoTu.Common
{
    /// <summary>
    /// 邮件类
    /// 修改者：罗国华
    /// 修改日期：2010.10.20
    /// </summary>
    public class SysMail
    {
        public SysMail() { }
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="sMailServer">邮件服务器</param>
        /// <param name="sMailServerUserName">服务器名称</param>
        /// <param name="sMailServerUserPassword">服务器密码</param>
        /// <param name="sFrom">发件人</param>
        /// <param name="sFromName">发件人姓名</param>
        /// <param name="sTo">收件人（多个,隔开）</param>
        /// <param name="sSubject">主题</param>
        /// <param name="sBody">内容</param>
        /// <returns></returns>
        public static bool SendEmail(string sMailServer, string sMailServerUserName, string sMailServerUserPassword
           , string sFrom, string sFromName, string sTo, string sSubject, string sBody)
        {
            return SendEmail(sMailServer, sMailServerUserName, sMailServerUserPassword, sFrom, sFromName, sTo, sSubject, sBody
                , true);
        }

        public static bool SendEmail(string sMailServer, string sMailServerUserName, string sMailServerUserPassword
            , string sFrom, string sFromName, string sTo, string sSubject, string sBody
            , bool isBodyHtml)
        {
            return SendEmail(sMailServer, sMailServerUserName, sMailServerUserPassword, sFrom, sFromName, sTo, sSubject, sBody
                , isBodyHtml, MailPriority.Normal);
        }

        public static bool SendEmail(string sMailServer, string sMailServerUserName, string sMailServerUserPassword
            , string sFrom, string sFromName, string sTo, string sSubject, string sBody
            , bool isBodyHtml, MailPriority mailPriority)
        {
            return SendEmail(sMailServer, sMailServerUserName, sMailServerUserPassword
                , sFrom, sFromName, sTo, sSubject, sBody
                , isBodyHtml, mailPriority, false);
        }
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="sMailServer">邮件服务器</param>
        /// <param name="sMailServerUserName">服务器名称</param>
        /// <param name="sMailServerUserPassword">服务器密码</param>
        /// <param name="sFrom">发件人</param>
        /// <param name="sFromName">发件人姓名</param>
        /// <param name="sTo">收件人（多个,隔开）</param>
        /// <param name="sSubject">主题</param>
        /// <param name="sBody">内容</param>
        /// <param name="isBodyHtml">html形式</param>
        /// <param name="mailPriority">优先级</param>
        /// <param name="enableSSL">是否使用套接字加密</param>
        /// <returns></returns>
        public static bool SendEmail(string sMailServer, string sMailServerUserName, string sMailServerUserPassword
            , string sFrom, string sFromName, string sTo, string sSubject, string sBody
            , bool isBodyHtml, MailPriority mailPriority, bool enableSSL)
        {
            try
            {
                //定义邮件发送内容
                System.Net.Mail.MailMessage myEmail = new System.Net.Mail.MailMessage();
                Encoding encode = Encoding.GetEncoding("utf-8");
                myEmail.From = new MailAddress(sFrom, sFromName, encode);
                myEmail.To.Add(sTo);//多个用,隔开
                myEmail.Subject = sSubject;
                myEmail.Body = sBody;
                myEmail.Priority = mailPriority;
                myEmail.IsBodyHtml = isBodyHtml;

                //定义邮件发送协议
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
                smtp.Host = sMailServer;
                //smtp.Port = "8080";
                smtp.Credentials = new System.Net.NetworkCredential(sMailServerUserName, sMailServerUserPassword);
                //smtp.UseDefaultCredentials = true;
                //smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtp.EnableSsl = enableSSL;


                smtp.Send(myEmail);
            }
            catch (System.Net.Mail.SmtpException e)
            {
                return false;
            }

            return true;
        }
    }
}
